#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Key.h"
#include "Timer.h"
#include "bsp_esp8266.h"
#include "bsp_esp8266_test.h"



iint main(void)
{
   /*初始化USART 配置模式为 115200 8-N-1，中断接收*/
   USART_Config();
   ESP8266_Init();

   printf("欢迎使用野火STM32开发板\n\n");
   printf("这是一个ESP8266AT指令测试实验\n\n");
   printf("请使用串口调试助手发送\"AT+换行回车\"测试ESP8266是否准备好\n\n");
   printf("更多AT指令请参考模块资料\n\n");
   printf("以下是ESP8266上电初始化打印的信息\n\n");

   while(1)
   {
         if(strUSART_Fram_Record .InfBit .FramFinishFlag == 1)  //如果接收到了串口调试助手的数据
         {
            strUSART_Fram_Record .Data_RX_BUF[strUSART_Fram_Record .InfBit .FramLength] = '\0';
            Usart_SendString(macESP8266_USARTx ,strUSART_Fram_Record .Data_RX_BUF);      //数据从串口调试助手转发到ESP8266
            strUSART_Fram_Record .InfBit .FramLength = 0;                                //接收数据长度置零
            strUSART_Fram_Record .InfBit .FramFinishFlag = 0;                            //接收标志置零
      }
         if(strEsp8266_Fram_Record .InfBit .FramFinishFlag)                             //如果接收到了ESP8266的数据
         {
            strEsp8266_Fram_Record .Data_RX_BUF[strEsp8266_Fram_Record .InfBit .FramLength] = '\0';
            Usart_SendString(DEBUG_USARTx ,strEsp8266_Fram_Record .Data_RX_BUF);        //数据从ESP8266转发到串口调试助手
            strEsp8266_Fram_Record .InfBit .FramLength = 0;                             //接收数据长度置零
            strEsp8266_Fram_Record.InfBit.FramFinishFlag = 0;                           //接收标志置零
         }
   }
}


// 单片机与电脑连接的串口中断服务函数
void DEBUG_USART_IRQHandler(void)
{
   uint8_t ucCh;

   /* 判断串口是否产生接收数据中断 */
   if ( USART_GetITStatus ( DEBUG_USARTx, USART_IT_RXNE ) != RESET )
   {
      ucCh  = USART_ReceiveData( DEBUG_USARTx );

      if ( strUSART_Fram_Record .InfBit .FramLength < ( RX_BUF_MAX_LEN - 1 ) )                       //预留1个字节写结束符
            strUSART_Fram_Record .Data_RX_BUF [ strUSART_Fram_Record .InfBit .FramLength ++ ]  = ucCh;

   }

   /* 判断串口是否产生空闲中断 */
   if ( USART_GetITStatus( DEBUG_USARTx, USART_IT_IDLE ) == SET )                //数据帧接收完毕
   {
      strUSART_Fram_Record .InfBit .FramFinishFlag = 1;

      ucCh = USART_ReceiveData( DEBUG_USARTx );                                  //由软件序列清除中断标志位(先读USART_SR，然后读USART_DR)
   }
}

// 单片机与ESP8266连接的串口服务函数
void macESP8266_USART_INT_FUN ( void )
{
   uint8_t ucCh;

   /* 判断串口是否产生接收数据中断 */
   if ( USART_GetITStatus ( macESP8266_USARTx, USART_IT_RXNE ) != RESET )
   {
      ucCh  = USART_ReceiveData( macESP8266_USARTx );

      if ( strEsp8266_Fram_Record .InfBit .FramLength < ( RX_BUF_MAX_LEN - 1 ) )       //预留1个字节写结束符
            strEsp8266_Fram_Record .Data_RX_BUF [ strEsp8266_Fram_Record .InfBit .FramLength ++ ]  = ucCh;
   }

   /* 判断串口是否产生空闲中断 */
   if ( USART_GetITStatus( macESP8266_USARTx, USART_IT_IDLE ) == SET )        //数据帧接收完毕
   {
      strEsp8266_Fram_Record .InfBit .FramFinishFlag = 1;

      ucCh = USART_ReceiveData( macESP8266_USARTx );                          //由软件序列清除中断标志位(先读USART_SR，然后读USART_DR)

   }
}
